package main

import (
	"fmt"
	"strconv"
)

//作业13 请将这段二进制翻译成中文(unicode编码), 并打印出翻译过程(比如 二进制: 1000 1000 0110 0011, unicode: U+8863, 字符: 衣)

func main() {

	fmt.Println(HumanBytesLoaded(1024))

}

// HumanBytesLoaded 单位转换(B KB MB GB TB EB)
func HumanBytesLoaded(bytesLength int64) string {
	var resp string

	// bytesLength
	//fmt.Printf("需要转换的值: %dB", bytesLength)
	res, nres := GetNum(float64(bytesLength), 0)

	switch nres {
	case 0:
		resp = strconv.FormatFloat(res, 'f', 0, 64) + "B"
	case 1:
		resp = strconv.FormatFloat(res, 'f', 2, 64) + "KB"
	case 2:
		resp = strconv.FormatFloat(res, 'f', 2, 64) + "MB"
	case 3:
		resp = strconv.FormatFloat(res, 'f', 2, 64) + "GB"
	case 4:
		resp = strconv.FormatFloat(res, 'f', 2, 64) + "TB"
	case 5:
		resp = strconv.FormatFloat(res, 'f', 2, 64) + "EB"
	}
	return resp
}

func GetNum(lenths float64, num int) (float64, int) {
	var res float64
	var nres int
	if num == 5 {
		res = lenths
		nres = num
	} else if lenths >= float64(1024) {
		lenths = lenths / float64(1024)
		num++
		res, nres = GetNum(lenths, num)
	} else {
		res = lenths
		nres = num
	}
	return res, nres
}
